//
//  MZDecimalDemoView.m
//  MZBaseDemo
//
//  Created by Chris Yang on 2016/12/5.
//  Copyright © 2016年 Chris Yang. All rights reserved.
//

#import "MZDemoSupprot.h"

#import "UIView+MZLayout.h"

#import "MZDecimalDemoView.h"

@implementation MZDecimalDemoView

- (id)init {
    
    self = [super init];
    
    if (self) {
        [self configSelf];
        [self allocSubviews];
        [self configSubviews];
        [self positionSubviews];
    }
    
    return self;
}

- (void)configSelf {
    [self setBackgroundColor:[UIColor whiteColor]];
}

- (void)allocSubviews {
    
    _leftField = [MZDemoSupprot mzDemoField];
    _opButton = [MZDemoSupprot mzDemoButton];
    _rightField = [MZDemoSupprot mzDemoField];
    _resultField = [MZDemoSupprot mzDemoField];
    
    _decimalCalcButton = [MZDemoSupprot mzDemoButton];
    _stringCalcButton = [MZDemoSupprot mzDemoButton];
    _numberCalcButton = [MZDemoSupprot mzDemoButton];
    
    _scaleButton = [MZDemoSupprot mzDemoButton];
}

- (void)configSubviews {
    
    /* Left Field */
    [_leftField setText:@"8id93"];
    [_leftField setPlaceholder:@"运算左值"];
    [_leftField setTextAlignment:NSTextAlignmentCenter];
    
    /* Operation Button */
    [_opButton setTitle:@"..." forState:UIControlStateNormal];
    [self button:_opButton addTargetWithTag:0];
    
    /* Right Field */
    [_rightField setText:@"3.86好i"];
    [_rightField setPlaceholder:@"运算右值"];
    [_rightField setTextAlignment:NSTextAlignmentCenter];
    
    /* Result Field */
    [_resultField setPlaceholder:@"运算结果"];
    [_resultField setTextAlignment:NSTextAlignmentCenter];
    [_resultField setUserInteractionEnabled:NO];
    
    /* Decimal Calc Button */
    [_decimalCalcButton setTitle:@"计算(D)" forState:UIControlStateNormal];
    [self button:_decimalCalcButton addTargetWithTag:1];
    
    /* String Calc Button */
    [_stringCalcButton setTitle:@"计算(S)" forState:UIControlStateNormal];
    [self button:_stringCalcButton addTargetWithTag:2];
    
    /* Number Calc Button */
    [_numberCalcButton setTitle:@"计算(N)" forState:UIControlStateNormal];
    [self button:_numberCalcButton addTargetWithTag:3];
    
    /* Scale Button */
    [_scaleButton setTitle:@"小数点" forState:UIControlStateNormal];
    [self button:_scaleButton addTargetWithTag:4];
        
    [self addSubview:_leftField];
    [self addSubview:_opButton];
    [self addSubview:_rightField];
    [self addSubview:_resultField];
    
    [self addSubview:_decimalCalcButton];
    [self addSubview:_stringCalcButton];
    [self addSubview:_numberCalcButton];
    [self addSubview:_scaleButton];
}

- (void)positionSubviews {
    
    CGRect tmpRect = CGRectZero;
    float tmpHeight = 40;
    float tmpSpace = 15;
    
    /* Scroll View */
    tmpRect = self.bounds;
    [self setFrame:tmpRect];
    
    /* Left Field */
    tmpRect = [self mzCenterRectInSubviewWithWidth:120 height:tmpHeight toTop:80];
    tmpRect.origin.x -= 90;
    [_leftField setFrame:tmpRect];
    
    /* Operation Button */
    tmpRect = [self mzCenterRectInSubviewWithWidth:tmpHeight height:tmpHeight toTop:80];
    [_opButton setFrame:tmpRect];
    [_opButton.layer setCornerRadius:tmpRect.size.width / 2];
    
    /* Right Field */
    tmpRect = [self mzCenterRectInSubviewWithWidth:120 height:tmpHeight toTop:80];
    tmpRect.origin.x += 90;
    [_rightField setFrame:tmpRect];
    
    /* Result Label */
    tmpRect = [self mzCenterRectInSubviewWithWidth:280 height:tmpHeight];
    tmpRect.origin.y = [_leftField mzGetYForBrotherViewToTopWithSpace:tmpSpace];
    [_resultField setFrame:tmpRect];
    
    /* Decimal Calc Button */
    tmpRect = [self mzCenterRectInSubviewWithWidth:80 height:tmpHeight];
    tmpRect.origin.x = _leftField.frame.origin.x;
    tmpRect.origin.y = [_resultField mzGetYForBrotherViewToTopWithSpace:tmpSpace];
    [_decimalCalcButton setFrame:tmpRect];
    
    /* String Calc Button */
    tmpRect.origin.x = [_decimalCalcButton mzGetXForBrotherViewToLeftWithSpace:tmpSpace];
    [_stringCalcButton setFrame:tmpRect];
    
    /* Number Calc Button */
    tmpRect.origin.x = [_stringCalcButton mzGetXForBrotherViewToLeftWithSpace:tmpSpace];
    [_numberCalcButton setFrame:tmpRect];
    
    /* Scale Button */
    tmpRect = [self mzCenterRectInSubviewWithWidth:280 height:tmpHeight];
    tmpRect.origin.y = [_decimalCalcButton mzGetYForBrotherViewToTopWithSpace:tmpSpace];
    [_scaleButton setFrame:tmpRect];
}

- (void)layoutSubviews {
    [self positionSubviews];
}

#pragma mark - Target-Action Pair

- (void)buttonClicked:(UIButton *)button {
    
    if ([_delegate respondsToSelector:@selector(demoView:buttonClickedAtIndex:)]) {
        [_delegate demoView:self buttonClickedAtIndex:button.tag];
    }
}

#pragma mark - Touch 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}

#pragma mark - MISC

- (void)button:(UIButton *)button addTargetWithTag:(NSInteger)tag {
    [button setTag:tag];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - Get

- (NSString *)leftVal {
    return _leftField.text;
}

- (NSString *)rightVal {
    return _rightField.text;
}

- (NSString *)resultVal {
    return _resultField.text;
}

#pragma mark - User Interface

- (void)updateOpString:(NSString *)opStr {
    [_opButton setTitle:opStr forState:UIControlStateNormal];
}

- (void)updateResult:(NSString *)result {
    [_resultField setText:result];
}

- (void)updateScale:(NSInteger)scale {
    
    NSString *tmpStr = [NSString stringWithFormat:@"小数位(%ld)", (long)scale];
    [_scaleButton setTitle:tmpStr forState:UIControlStateNormal];
}

@end

